package santiago.domain;

import java.io.Serializable;
import santiago.exception.InvalidOperationException;

public class Field implements Serializable {

    private Plantation Plantation;
    private boolean PalmTree;

    public Field(boolean PalmTree) {
        Plantation = null;
        this.PalmTree = PalmTree;
    }

    /**
     * Dry up a plantation until it turn into desert
     */
    public void dryUp() throws InvalidOperationException {
        if (Plantation == null) {
            throw new InvalidOperationException();
        }
        try {
            Plantation.dryUp();
        } catch (InvalidOperationException e) {
            if (PalmTree) {
                PalmTree = false;
            } else {
                Plantation.turnIntoDesert();
            }
        }
    }

    /**
     * Turn a field into desert
     */
    public void turnIntoDesert() {
        PalmTree = false;
        Plantation.turnIntoDesert();
    }

    /**
     * @return Score of the field, counting 1 point by YieldMarker and Palmtree
     */
    public byte getScore() {
        if (Plantation != null) {
            return (byte) (Plantation.getScore() + (byte) (PalmTree ? 1 : 0));
        } else {
            return 0;
        }
    }

    public void setPlantation(Plantation Plantation) {
        this.Plantation = Plantation;
    }

    public Plantation getPlantation() {
        return Plantation;
    }

    public boolean isDesert() {
        return hasPlantation() ? Plantation.isDesert() : false;
    }

    public boolean hasPlantation() {
        return Plantation != null;
    }

    public boolean hasPlamTree() {
        return PalmTree;
    }
}
